package com.lx.dao.imp;

import com.lx.bean.Courier;
import com.lx.bean.User;
import com.lx.dao.CourierDao;
import com.lx.util.DruidUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: UserDaoImpl
 * @Description:
 * @Date Created in  2021-07-09 19:06
 * @Modified By:
 * @Author: 零巽
 */
public class CourierDaoImpl implements CourierDao {
    private static final String SQL_GET_ALL_COURIER = "SELECT * FROM COURIER";
    private static final String SQL_GET_COURIER_LIMIT = "SELECT * FROM COURIER LIMIT ?,?";
    private static final String SQL_ADD = "INSERT INTO COURIER  (ID,USERNAME,PHONE,IDNUMBER,PASSWORD,REGISTERTIME)  VALUES(null,?,?,?,?,NOW())";
    private static final String SQL_FIND_BY_PHONE = "SELECT * FROM COURIER WHERE PHONE=?";
    private static final String SQL_UPDATE = "UPDATE COURIER SET USERNAME=?,PHONE=?,IDNUMBER=?,PASSWORD=? WHERE ID=?";
    private static final String SQL_DELETE_BY_PHONE = "DELETE FROM COURIER WHERE PHONE=?";
    private static final String SQL_COUNT_CONSOLE = "SELECT COUNT(*) total,COUNT(TO_DAYS(REGISTERTIME)=TO_DAYS(NOW()) OR NULL) today FROM COURIER";
    /**
     * 获取快递员列表的方法
     *
     * @param limit    是否分页，true表示是
     * @param offset   索引起始值
     * @param pageSize 每页条数
     * @return 包含用户的List
     */
    @Override
    public List<Courier> getAllCourier(Boolean limit, int offset, int pageSize) {
        //获取连接
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Courier> list = new ArrayList<>();
        //预编译SQL语句
        try {
            if (limit) {
                preparedStatement = connection.prepareStatement(SQL_GET_COURIER_LIMIT);
                //传参
                preparedStatement.setInt(1, offset);
                preparedStatement.setInt(2, pageSize);
            } else {
                preparedStatement = connection.prepareStatement(SQL_GET_ALL_COURIER);
            }
            //执行SQL语句得到结果
            resultSet = preparedStatement.executeQuery();
            //根据结果集得到结果
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String userName = resultSet.getString("userName");
                String phone = resultSet.getString("phone");
                String idNumber = resultSet.getString("idNumber");
                String password = resultSet.getString("password");
                int sendNumber = resultSet.getInt("sendNumber");
                Timestamp registerTime = resultSet.getTimestamp("registerTime");
                Timestamp loginTime = resultSet.getTimestamp("loginTime");
                Courier courier = new Courier(id, userName, phone, idNumber, password,sendNumber, registerTime, loginTime);
                list.add(courier);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭资源
            DruidUtil.close(connection, preparedStatement, resultSet);
        }
        return null;
    }

    /**
     * 添加用户的方法
     *
     * @param courier 需要添加的用户对象
     * @return ture表示添加成功
     */
    @Override
    public boolean addCourier(Courier courier) {
        //获取连接
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        //预编译SQL语句
        try {
            preparedStatement = connection.prepareStatement(SQL_ADD);
            //传参
            preparedStatement.setString(1,courier.getUserName());
            preparedStatement.setString(2, courier.getPhone());
            preparedStatement.setString(3,courier.getIdNumber());
            preparedStatement.setString(4,courier.getPassword());
            //执行SQL语句得到结果
            int i = preparedStatement.executeUpdate();
            //返回结果
            return i>0?true:false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭资源
            DruidUtil.close(connection, preparedStatement, null);
        }
        return false;
    }

    /**
     * 通过手机号查找用户
     *
     * @param phone 手机号
     * @return 找到的用户对象，没找到返回null
     */
    @Override
    public Courier findByPhone(String phone) {
        //获取连接
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet=null;
        Courier courier = null;
        //预编译SQL语句
        try {
            preparedStatement = connection.prepareStatement(SQL_FIND_BY_PHONE);
            //传参
            preparedStatement.setString(1,phone);
            //执行SQL语句得到结果
            resultSet = preparedStatement.executeQuery();
            //返回结果
            if (resultSet.next()) {
                int id = resultSet.getInt("id");
                String userName = resultSet.getString("userName");
                String idNumber = resultSet.getString("idNumber");
                String password = resultSet.getString("password");
                Timestamp registerTime = resultSet.getTimestamp("registerTime");
                Timestamp loginTime = resultSet.getTimestamp("loginTime");
                courier = new Courier(id, userName, phone, idNumber, password, registerTime, loginTime);
            }
            return courier;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭资源
            DruidUtil.close(connection, preparedStatement, resultSet);
        }
        return null;
    }

    /**
     * 修改用户的方法
     *
     * @param id   需要修改的用户id
     * @param courier 修改后的用户对象
     * @return true表示修改成功
     */
    @Override
    public boolean update(int id, Courier courier) {
        //获取连接
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        //预编译SQL语句
        try {
            preparedStatement = connection.prepareStatement(SQL_UPDATE);
            //传参
            preparedStatement.setString(1, courier.getUserName());
            preparedStatement.setString(2,courier.getPhone());
            preparedStatement.setString(3, courier.getIdNumber());
            preparedStatement.setString(4,courier.getPassword());
            preparedStatement.setInt(5, id);
            //执行SQL语句得到结果
            int i = preparedStatement.executeUpdate();
            //返回结果
            return i>0?true:false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭资源
            DruidUtil.close(connection, preparedStatement, null);
        }
        return false;
    }

    /**
     * 通过电话删除用户的方法
     *
     * @param phone 需要删除的用户电话
     * @return true表示修改成功
     */
    @Override
    public boolean deleteByPhone(String phone) {
        //获取连接
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        //预编译SQL语句
        try {
            preparedStatement = connection.prepareStatement(SQL_DELETE_BY_PHONE);
            //传参
            preparedStatement.setString(1,phone);
            //执行SQL语句得到结果
            int i = preparedStatement.executeUpdate();
            //返回结果
            return i>0?true:false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭资源
            DruidUtil.close(connection, preparedStatement, null);
        }
        return false;
    }

    /**
     * 查询总用户数和新增用户数量
     *
     * @return 返回的数组第一个值为总数，第二个值为新增数量
     */
    @Override
    public int[] console() {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int[] console = new int[2];
        try {
            preparedStatement = connection.prepareStatement(SQL_COUNT_CONSOLE);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                int total = resultSet.getInt(1);
                int today = resultSet.getInt(2);
                console[0] = total;
                console[1] = today;
            }
            return console;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement,resultSet);
        }
        return null;
    }
}
